package com.ailanyin.modules.flowable.controller;

import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.modules.flowable.domain.bo.WfModelBo;
import com.ailanyin.modules.flowable.domain.vo.WfModelVo;
import com.ailanyin.modules.flowable.service.IWfModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 工作流流程模型管理
 *
 * @author KonBAI
 * @createTime 2022/6/21 9:09
 */
@Slf4j
@RestController
@RequestMapping("/workflow/model")
public class WfModelController {

    @Autowired
    private IWfModelService modelService;

    @GetMapping("/list")
    public CommonPage<WfModelVo> list(WfModelBo modelBo,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return modelService.list(modelBo, pageNum, pageSize);
    }

    @GetMapping("/historyList")
    public CommonPage<WfModelVo> historyList(WfModelBo modelBo,
                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return modelService.historyList(modelBo, pageNum, pageSize);
    }

    /**
     * 获取流程模型详细信息
     *
     * @param modelId 模型主键
     */
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        return AjaxResult.success(modelService.getModel(modelId));
    }

    /**
     * 获取流程表单详细信息
     *
     * @param modelId 模型主键
     */
    @GetMapping(value = "/bpmnXml/{modelId}")
    public AjaxResult getBpmnXml(@PathVariable("modelId") String modelId) {
        return AjaxResult.success("操作成功", modelService.queryBpmnXmlById(modelId));
    }

    /**
     * 新增流程模型
     */
    @PostMapping
    public AjaxResult add(@RequestBody WfModelBo modelBo) {
        modelService.insertModel(modelBo);
        return AjaxResult.success();
    }

    /**
     * 修改流程模型
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WfModelBo modelBo) {
        modelService.updateModel(modelBo);
        return AjaxResult.success();
    }

    /**
     * 保存流程模型
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody WfModelBo modelBo) {
        modelService.saveModel(modelBo);
        return AjaxResult.success();
    }

    /**
     * 设为最新流程模型
     *
     * @param modelId
     * @return
     */
    @PostMapping("/latest")
    public AjaxResult latest(@RequestParam String modelId) {
        modelService.latestModel(modelId);
        return AjaxResult.success();
    }

    /**
     * 删除流程模型
     *
     * @param modelIds 流程模型主键串
     */
    @DeleteMapping("/{modelIds}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] modelIds) {
        modelService.deleteByIds(Arrays.asList(modelIds));
        return AjaxResult.success();
    }

    /**
     * 部署流程模型
     *
     * @param modelId 流程模型主键
     */
    @PostMapping("/deploy")
    public AjaxResult deployModel(@RequestParam String modelId) {
        return AjaxResult.toAjax(modelService.deployModel(modelId) ? 1 : 0);
    }

}
